import sys,os
sys.path.append(os.path.realpath('../myfunctions'))
import fast_network_analysis as fna
import netclass as nc
import net_analysis as na
import single_cell_analysis as ssa

import shelve
import numpy
from scipy import *
import scipy.io as io
from brian import *
import time


pathfolder= '../datasave/'
filename='2013-5-12-17-39'
dt=0.0001

filename_analysis=na.name_file()

#pathfile='../datasave/'+filename
#param=shelve.open(pathfile+'-param.db',protocol=-2)
#import pdb;pdb.set_trace()

#spdict=na.loadSpikes_AER(filename,pathfolder)

rates=na.loadRates(filename,pathfolder)
#na.plotPopRates(rates)

spdict=na.loadSpikes(filename,pathfolder)
spcov_mean= na.correlationcalc(spdict,rates,filename,pathfolder,200,1*ms,20*ms,rates['simpar']['duration'])
na.saveCorr(filename,filename_analysis,spcov_mean)
#na.plot_correlations(spcov_mean)

import pdb; pdb.set_trace()
#spdict= na.loadResults(filename,pathfolder)



mon_e, mon_c, mon_i, time = na.loadMonitors(filename,pathfolder)

print 'Recovering voltage exc...'
vtrace_nsp_e=na.recover_voltage('exc',filename,pathfolder)
print 'Recovering voltage clu...'
vtrace_nsp_c=na.recover_voltage('clu',filename,pathfolder)

vtrace_e=transpose(mon_e['voltage'])
vtrace_c=transpose(mon_c['voltage'])


eventsize=0.001 # minumum event detected

print 'Measuring large events...'
celltype='exc'
large_event_e = na.large_event_network(filename,vtrace_e,spdict,eventsize,celltype,'yes')
celltype='clu'
large_event_c = na.large_event_network(filename,vtrace_c,spdict,eventsize,celltype,'yes')

na.saveLargeEvents(filename,filename_analysis,large_event_e,'exc')
na.saveLargeEvents(filename,filename_analysis,large_event_c,'clu') 

celltype='exc'
large_event_e_nosp = na.large_event_network(filename,vtrace_nsp_e,spdict,eventsize,celltype,'no')
celltype='clu'
large_event_c_nosp = na.large_event_network(filename,vtrace_nsp_c,spdict,eventsize,celltype,'no')
na.saveLargeEvents(filename,filename_analysis,large_event_e_nosp,'exc_nsp')
na.saveLargeEvents(filename,filename_analysis,large_event_c_nosp,'clu_nsp')

na.plot_comparison_summary(large_event_e,large_event_c)
na.plot_comparison_summary(large_event_e,large_event_e_nosp)
na.plot_comparison_summary(large_event_c,large_event_c_nosp)
na.plot_comparison_summary(large_event_e_nosp,large_event_c_nosp)




